package com.lc.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lc.yygh.hosp.repository.DepartmentRepository;
import com.lc.yygh.hosp.service.DepartmentService;
import com.lc.yygh.model.hosp.Department;
import com.lc.yygh.model.hosp.Hospital;
import com.lc.yygh.vo.hosp.DepartmentQueryVo;
import com.lc.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void save(Map<String, Object> paramMap) {
        Department dep=JSONObject.parseObject(JSONObject.toJSONString(paramMap),Department.class);
        //从数据库查询一个科室
        Department department = departmentRepository.findByHoscodeAndDepcode(dep.getHoscode(), dep.getDepcode());
        if(null != department) {
            //科室已存在 做更新
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            //不存在则插入一个科室
            Date now=new Date();
            dep.setCreateTime(now);
            dep.setUpdateTime(now);
            dep.setIsDeleted(0);
            departmentRepository.save(dep);
        }
    }

    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo dvo) {
        Pageable pageable= PageRequest.of(page-1,limit);
        Department department=new Department();
        //拷贝属性的值
        BeanUtils.copyProperties(dvo,department);
        department.setIsDeleted(0);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        //创建实例
        Example<Department> example=Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            departmentRepository.deleteById(department.getId());//deleteById是封装好的方法
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //封装最终数据的集合
        List<DepartmentVo>list=new ArrayList<>();
        //根据医院编号，查询医院所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example example = Example.of(department);
        //所有科室列表 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);
        //把科室列表数据按bigcode分组
        Map<String, List<Department>> deptMap = departmentList.stream().collect(Collectors.groupingBy(d -> d.getBigcode()));
        for (Map.Entry<String, List<Department>> entry : deptMap.entrySet()) {
            //获取大科室编码bigcode
            String bigcode=entry.getKey();
            //与bigcode对应的科室列表(一对多)
            List<Department> value = entry.getValue();
            String bigname = value.get(0).getBigname();//得到大科室名称
            //封装大科室
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(bigname);
            //创建小科室的集合
            List<DepartmentVo>children=new ArrayList<>();
            for (Department dep : value) {
                //没循环一次就拿到一个小科室
                DepartmentVo dvo=new DepartmentVo();
                dvo.setDepcode(dep.getDepcode());
                dvo.setDepname(dep.getDepname());
                children.add(dvo);
            }
            //把小科室集合设置到大科室中
            departmentVo.setChildren(children);
            //把大科室(下级有小科室)添加到集合中
            list.add(departmentVo);//整个要返回的数据(大科室下有小科室)
        }
        return list;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }
}


